<?php
namespace Mob \ Controller;

use Think \ Controller;
use Think \ Hook;

class StudentController extends BaseController {

    private $UserInfo;

	public function _initialize() {
		parent :: _initialize();
		$this->_top_menu_list = array (
			'left' => array (
				array (
					'type' => 'home',
					'href' => U('Mob/Activity/activitymap'
				)
			),


		), 'center' => array (
			'title' => '知道'
		));
		if (is_login()) {


	  $this->_top_menu_list['right'][] = array (
					'type' => 'edit',
					'href' => U('Mob/Activity/creator'
				));

		} else {
			$this->_top_menu_list['right'][] = array (
				'type' => 'edit',
				'info' => '登录后才能操作！'
			);

		$this->redirect('Member/index');


		}
		//dump($this->_top_menu_list);exit;



		$this->UserInfo = query_user(array('nickname', 'avatar64', 'uid'), $v['uid']);
		$this->setMobTitle('知道');
		$this->assign('top_menu_list', $this->_top_menu_list);
	//print_r($this->_top_menu_list);
	}

	/**
	 * 主页面显示
	 */
	public function index() {


    $this->display();



	}

    //注册页面
	public function register()
	{

	if(IS_POST)
	{
	$name = I('post.studentName','','string');
	$studentId = I('post.studentId','','int');
	$latitude = I('post.latitude');
	$longitude = I('post.longitude');
	$schooId = I('post.schooId',10183,'int');
	$campus = I('post.campus','','string');
	$dormitory= I('post.dormitory','','string');
	$dorm = I('post.dorm','','string');
	$profession= I('post.profession','','string');
	$sex = I('post.sex',null,'string');

	$uid = get_uid();
	$student = D('Student');
	$is_reg = $student->is_verific($uid);
	$is_regSid = $student->is_ExistSid($studentId,$schooId);

    if($is_regSid)
    {
     $data['status']=0;
   	 $data['msg']='学号已经被注册过了!';
    }

	elseif($is_reg)
	{
	 if($is_reg['verificFlag']==1)
	 {
	 $data['status']=0;
   	 $data['msg']='已经注册过了!';
	 }elseif($is_reg['verificFlag']==0)
	 {
     $data['status']=0;
   	 $data['msg']='已经提交，正在等待确认中!';

	 }


	}else
	{

	$wxUser = D('WxUser')->getUserInfo($uid);
	$nickname = $wxUser['nickname'];
	$openid= $wxUser['OpenId'];
	$res = $student->register($uid,$openid,$name,$nickname,$studentId,$latitude,$longitude,$schooId,$campus,$dormitory,$dorm,$profession,$sex);
	if($res)
	{
	 $data['status']=1;
  	 $data['msg']='提交成功!';
   // $data['msg']=M()->getLastSql();
	}else
	{
	 $data['status']=0;
	 $data['msg']='提交失败!';
    // $data['msg']=M()->getLastSql();
	}


	}
    $this->ajaxReturn($data);

	}else
	{
	$uid = get_uid();
	$errmsg = I('get.errmsg',null);
	$student = D('Student');
    $studentInfo = $student->is_Student($uid);
    if($studentInfo)
    {
    $is_reg = 1;
    $errmsg='已经注册过了!';

    }else
    {
    $is_reg = 0;
    }
  //  print_r($is_reg);
    $this->assign('errmsg',$errmsg);
    $this->assign('is_reg',$is_reg);
    $this->assign('student',$studentInfo);
    $this->display();

	}
	}

	public function userInfo()
	{
	 $uid = I('get.uid',null);
    if(empty($uid)) $uid=get_uid();

    $student = D('Student');
    $stuentInfo = $student->queryStudent($uid);

    if(empty($stuentInfo))
    {
    $stuentInfo = $student->queryStudent(get_uid());
    }

    $user =  query_user(array('nickname', 'avatar64', 'uid'), $stuentInfo['Uid']);
    $this->assign('stuentInfo',$stuentInfo);
    $this->assign('user',$user);
   // print_r($stuentInfo);
    $this->display();

	}

    public function verificOper()
    {
     $teacherId = get_uid();
     $studentId = I('post.studentId',null,'int');
     $verifyFlg = I('post.flg',null,'int');
     $classId = I('post.ClassId',null,'int');
     $studentUid= I('post.studentUid',null,'int');
     $student = D('Student');
     $verify = $student->queryStudent($studentUid);
     if($verify['verificFlag']==$verifyFlg)
     {
     $data['status']=0;
   	 $data['msg']='状态已更新!';
   	 $status=0;
   	 $msg = '重复操作';

     }else
     {
     $res = $student->verifyTeacherAuth($studentId,$teacherId);

     if($res)
     {
     if($student->verifyOper($studentUid,$verifyFlg,$teacherId,$teacherId))
     {
      $data['status']=1;
   	  $data['msg']='操作成功!';
   	 $status=1;
   	 $msg = $data['msg'];

     }else
     {
      $data['status']=0;
      $data['msg']='操作失败!';
    //	$data['msg']= M()->getLastSql();
      $status=0;
   	 $msg = $data['msg'];
     }


     }else
     {
      $data['status']=0;
   	 $data['msg']='您没有管理该学生权限!';
    	//$data['msg']=M()->getLastSql();
   	 $status=0;
   	 $msg = $data['msg'];
     }

     }
     $student->verifyRecord($studentUid,$studentId,$teacherId,$verifyFlg,$status,$msg);
     $this->ajaxReturn($data);

    }

    public function updateDormLocation()
    {
     $uid = get_uid();
    if(IS_POST)
    {
    $longitude = I('post.longitude',null,'float');
    $latitude = I('post.latitude',null,'float');
    $student = D('Student');
    $student_info = $student->queryStudent($uid);
    $srcLongitude=$student_info['longitude']; //获取原注册的位置
    $srcLatitude=$student_info['latitude'];
    $res = $student->updateDormLocation($uid,$longitude,$latitude);
    if($res)
    {
    $data = array('status'=>1,'msg'=>'更新成功');
    $array = array('Uid'=>$uid,'modifyTime'=>time(),'srcLongitude'=>$srcLongitude,'srcLatitude'=>$srcLatitude,'desLongitude'=>$longitude,'desLatitude'=>$latitude,'status'=>1,'studentId'=>$student_info['studentId']);
    M('dorm_updaterecord')->add($array);
    }else
    {
    $data = array('status'=>0,'msg'=>'操作失败');
   // $data = array('status'=>0,'msg'=>M()->getLastSql());
    }
    $this->ajaxReturn($data);

    }else
    {
    $errmsg = I('get.errmsg',null);
    $studentInfo = D('Student')->queryStudent($uid);
    if(empty($studentInfo))
    {
      $this->redirect('Student/register',array('errmsg'=>'请绑定班组后，再签到操作！'));
    }elseif($studentInfo['verificFlag']==0)
    {
     $this->redirect('Student/register',array('errmsg'=>'请审核通过后再修改！'));
    }
    $class= D('Orga')->get_OrgaFullInfo($studentInfo['ClassId']);
    $this->assign('errmsg',$errmsg);
    $this->assign('studentInfo',$studentInfo);
    $this->assign('class',$class);
    $this->display();
    }
    }

    public function batchVerificOper()
    {
     $uid = get_uid();
     $classId = I('post.classId',null,'int');

     $operateFlg = I('post.operateFlg',null,'int');
     $schooId = I('post.schooId','10183','int');

     $is_auth = D('Student')->verifyTeacherClassAuth($uid,$classId);
     if($is_auth)
     {
     $res = D('Student')->classBatchVerific($uid,$classId,$operateFlg,$schooId);
     if($res)
     {
      $data = array('status'=>1,'msg'=>'操作成功');
     }else
     {
       $data = array('status'=>0,'msg'=>'没有要审批的记录');

     }


     }else
     {
     $data = array('status'=>0,'msg'=>'操作失败');
     }
     $this->ajaxReturn($data);


    }

    public function removeOperate()
    {
     if(IS_POST)
     {
     $studentId = I('post.studentId',null,'int');
     $uid = get_uid();

     $is_auth = D('Student')->verifyTeacherAuth($studentId,$uid);

     if($is_auth)
     {
     $res = D('Student')->removeOperate($uid,$studentId);
     if($res)
     {
     $data = array('status'=>1,'msg'=>'操作成功');
     }
     else
     {
     $data = array('status'=>0,'msg'=>'操作失败');
     }
     }else
     {
     $data = array('status'=>0,'msg'=>'没有权限');
   /// $data = array('status'=>0,'msg'=>M()->getLastSql());
     }
    $this->ajaxReturn($data);

     }

    }
    public function cancelApply()
    {
    $uid = get_uid();
    $student = D('Student');
    $studentInfo = $student->is_Student($uid);
    if(empty($studentInfo))
    {
   $data = array('status'=>0,'msg'=>'信息不存在!');
    }else
    {
    $res = $student->removeOperate($uid,$studentInfo['studentId'],2);
    if($res)
    {
    $data = array('status'=>1,'msg'=>'操作成功!');
    }else
    {
    $data = array('status'=>0,'msg'=>'操作失败!');
    }
   }
    $this->ajaxReturn($data);
    }

    public function information()
    {
    $uid = I('get.uid',null,'int');
    $uid = empty($uid)?get_uid():$uid;
    $user_Info = query_user(array('avatar64', 'nickname', 'uid', 'space_url','space_mob_url', 'icons_html', 'score', 'title', 'fans', 'following', 'weibocount', 'rank_link', 'signature'), $uid);
    $student_Info = D('Student')->is_Student($uid);
    $student_Info['class'] = D('Orga')->get_OrgaFullInfo($student_Info['ClassId']);
    $this->assign('userInfo',$user_Info);
    $this->assign('studentInfo',$student_Info);
    $this->display();
    }

    public function resetDormLocation()
    {
    if(IS_POST)
    {
    $studentId = I('post.studentId',null,'string');
    $longitude = I('post.longitude',null,'string');
    $latitude = I('post.latitude',null,'string');
    $teacherUid = get_uid();
    $is_auth = D('Student')->verifyTeacherAuth($studentId,$teacherUid);
    if(!$is_auth)
    {
    $data = array('status'=>0,'msg'=>'您没有修改权限!');
    }else
    {
    $data = array('longitude'=>$longitude,'latitude'=>$latitude);
    $res = D('Student')->updateStudentInfo($studentId,$data);
    if($res)
    {
    $data = array('status'=>1,'msg'=>'操作成功!');
    }else
    {
     $data = array('status'=>0,'msg'=>'操作失败!');
    }

   }
    $this->ajaxReturn($data);

    }else
    {
    $StudentUid = I('get.StudentUid',null,'int');  //获取学生ID
    $teacherUid = get_uid();  //获取老师ID
    $student = D('Student');   //实例化模型
    $Student_Info = $student->queryStudent($StudentUid);//获取学生信息
    $is_auth = $student->verifyTeacherAuth($Student_Info['studentId'],$teacherUid);
    if($is_auth)
    {
    $this->assign('student',$Student_Info);
    $this->display();
    }else
    {
    $this->display();
    }

    }








    }





}



